package cn.itsource.service.impl;

import cn.itsource.common.AjaxResult;
import cn.itsource.common.PageResult;
import cn.itsource.domain.TEmployee;
import cn.itsource.mapper.EmployeeMapper;
import cn.itsource.query.EmployeeQuery;
import cn.itsource.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Effect:
 * date: 2022/1/13 19:21
 *
 * @author wangyu
 * @since JDK 1.8
 */
@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public List<TEmployee> findAll() {
        return employeeMapper.finAll();
    }

    @Override
    public List<TEmployee> findEmpList() {
        return employeeMapper.findEmpList();
    }

    @Override
    public PageResult<TEmployee> findByQuery(EmployeeQuery employeeQuery) {
        PageHelper.startPage(employeeQuery.getPageNum(), employeeQuery.getPageSize());

        PageInfo<TEmployee> tEmployeePageInfo = new PageInfo<>(employeeMapper.findByQuery(employeeQuery));

        return new PageResult<>(tEmployeePageInfo.getTotal(),tEmployeePageInfo.getList());
    }

    @Override
    public AjaxResult del(Long id) {
        try {
            employeeMapper.del(id);
            return AjaxResult.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除失败");
        }

    }

    @Override
    public AjaxResult saveAndUpt(TEmployee employee) {
        try {
            if (employee.getId() == null || "".equals(employee.getId())){
                employeeMapper.addEmployee(employee);
            }else{
                employeeMapper.uptEmployee(employee);
            }
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }

    }

    @Override
    public TEmployee findEmpById(Long id) {
        return employeeMapper.findEmpById(id);
    }
}
